package com.kk.base.controller.admin;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.admin.BuyOff;
import com.kk.base.entity.common.ExpressMailOrderStatus;
import com.kk.base.entity.home.ExpressMailOrder;
import com.kk.base.service.admin.BuyOffService;
import com.kk.base.service.home.ExpressMailOrderService;
import com.kk.base.service.admin.ProcessRecordsService;
import com.kk.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/expressMailOrder")
public class AdminExpressMailOrderController {



    @Autowired
    private ExpressMailOrderService expressMailOrderService;

    @Autowired
    private ProcessRecordsService processRecordsService;

    @Autowired
    private BuyOffService buyOffService;


    @RequestMapping("/list")
    public String list(Model model, ExpressMailOrder expressMailOrder, PageBean<ExpressMailOrder> pageBean){
        model.addAttribute("waybillNumber",expressMailOrder.getWaybillNumber());
        model.addAttribute("title", "揽收列表");
        model.addAttribute("pageBean",expressMailOrderService.findAllByWaybillNumber(expressMailOrder,pageBean));
        return "admin/express_mail_order/list";
    }


    /**
     * 订单编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value="/edit",method= RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id",required=true)Long id){
        BuyOff buyOff = buyOffService.find(id);
        model.addAttribute("expressMailOrder",buyOff.getBuyOffExpressMailOrder());
        return "admin/buy_off/edit";
    }

    /**
     * 编辑订单信息信息表单提交处理
     * @param expressMailOrder
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(ExpressMailOrder expressMailOrder){
        //校验数据是否合法
        if (!StringUtil.isMobile(expressMailOrder.getRecipientsMobile()) || null == expressMailOrder.getRecipientsMobile()
            || "".equals(expressMailOrder.getRecipientsMobile())){
            return  Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ISMOBILE_EDIT_ERROR);
        }

        if ("".equals(expressMailOrder.getRecipientsAddress()) || null == expressMailOrder.getRecipientsAddress()){
            return  Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ISADDRESS_EDIT_ERROR);
        }


        if (expressMailOrder.getRecipientsAddress().length() > 50){
            return  Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ISADDRESSLENGTH_EDIT_ERROR);
        }

        ExpressMailOrder expressMailOrderById = expressMailOrderService.find(expressMailOrder.getId());
        expressMailOrderById.setRecipientsMobile(expressMailOrder.getRecipientsMobile());
        expressMailOrderById.setRecipientsAddress(expressMailOrder.getRecipientsAddress());
        expressMailOrderById.setRecipientsFullAddress(expressMailOrder.getRecipientsFullAddress());

        //一切合法进行保存
        if(expressMailOrderService.save(expressMailOrderById) == null){
            return Result.error(CodeMsg.ADMIN_USE_EDIT_ERROR);
        }

        return Result.success(true);
    }



    @RequestMapping("/takeDeliveryOfGoods")
    @ResponseBody
    public Result<Boolean> takeDeliveryOfGoods(@RequestParam(name="id",required=true)Long id){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_USER_NULL);
        }

        if(1 == expressMailOrderService.saveExpressMailOrderAndBuyOff(id)){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_RECEIVING_EDIT_ERROR);
        }

        return Result.success(true);
    }


    @RequestMapping("/sendTheCargo")
    @ResponseBody
    public Result<Boolean> sendTheCargo(@RequestParam(name="id",required=true)Long id,
                                        @RequestParam(name="userId",required=true)Long userId){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ID_NULL);
        }

        if (null == userId){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_USER_NULL);
        }

        Integer integer = processRecordsService.ChangeStatusSaveProcess(id, userId,
                ExpressMailOrderStatus.THEGOODS, ExpressMailOrderStatus.COMPLETED);

        if(integer == 1){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SENDTHECARGO_ADD_EDIT_ERROR);
        }
        return Result.success(true);
    }

}
